package com.masterclient.app.modules.cart.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.masterclient.app.R;
import com.masterclient.app.modules.cart.beans.DealerProductsBean;

/**
 * 作者：hxm
 * 创建时间：2017/8/4 17:24
 * 功能说明：
 */
public class ShopCartCollectAdapter extends BaseQuickAdapter<DealerProductsBean, BaseViewHolder> {

    private Context mContent;
    private ShopCartCollectAdapterListener listener;
    private int size;

    public ShopCartCollectAdapter(Context content, int size, ShopCartCollectAdapterListener listener) {
        super(R.layout.item_shopcart_collect);
        mContent = content;
        this.listener = listener;
        this.size = size;
    }

    @Override
    protected void convert(BaseViewHolder helper, DealerProductsBean item) {
        View viewItem = helper.getView(R.id.view_item);
        View viewHead = helper.getView(R.id.view_head);
        View viewAllLine = helper.getView(R.id.view_all_line);
        View viewHalfLine = helper.getView(R.id.view_half_line);
        View viewPoint = helper.getView(R.id.view_point);
        CheckBox checkBox = helper.getView(R.id.checkBox);
        TextView tvShopName = helper.getView(R.id.tv_shop_name);
        TextView tvTotalNum = helper.getView(R.id.tv_total_num);
        RecyclerView recyclerView = helper.getView(R.id.recyclerView);

        //分类头部
        if ("4".equals(item.type)) {
            viewHead.setVisibility(View.GONE);
        } else {
            viewHead.setVisibility(View.VISIBLE);
        }

        //是否被选中
        checkBox.setChecked(item.isChecked);
        if (item.isChecked) {
            viewPoint.setBackgroundResource(R.drawable.shap_red_point);
        } else {
            viewPoint.setBackgroundResource(R.drawable.shap_gray_point);
        }
        checkBox.setOnClickListener(v -> {
            if (checkBox.isChecked()) {
                listener.onCheckShop(helper.getLayoutPosition(), true,item.saleType);
            } else {
                listener.onCheckShop(helper.getLayoutPosition(), false,item.saleType);
            }
        });

        //虚线显示
        if (("1".equals(item.type) || "2".equals(item.type)) && item.isChecked) {
            viewHalfLine.setVisibility(View.VISIBLE);
            viewAllLine.setVisibility(View.GONE);
        } else if ("3".equals(item.type) && item.isChecked) {
            viewHalfLine.setVisibility(View.VISIBLE);
            viewAllLine.setVisibility(View.VISIBLE);
        } else if ("4".equals(item.type) && item.isChecked) {
            if (helper.getLayoutPosition() == size - 1) {
                viewHalfLine.setVisibility(View.VISIBLE);
                viewAllLine.setVisibility(View.GONE);
            } else {
                viewHalfLine.setVisibility(View.VISIBLE);
                viewAllLine.setVisibility(View.VISIBLE);
            }
        } else {
            viewHalfLine.setVisibility(View.GONE);
            viewAllLine.setVisibility(View.GONE);
        }

        //店铺名称
        if (item.seller != null && !TextUtils.isEmpty(item.seller.storeName)) {
            tvShopName.setText(item.seller.storeName);
        }

        //店铺商品总数
        if (!TextUtils.isEmpty(item.totalCartNum)) {
            String str = "以下共计 "+item.popCartNum+"件"+" 商品";
            int end= 6+item.popCartNum.length();
            SpannableStringBuilder style=new SpannableStringBuilder(str);
            style.setSpan(new ForegroundColorSpan(Color.parseColor("#F23051")),5,end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            tvTotalNum.setText(style);
        }

        //店铺商品列表
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 4);
        recyclerView.setLayoutManager(layoutManager);
        ShopCartCollectImageAdapter shopCartCollectImageAdapter = new ShopCartCollectImageAdapter(mContent);
        recyclerView.setAdapter(shopCartCollectImageAdapter);
        shopCartCollectImageAdapter.bindToRecyclerView(recyclerView);
        shopCartCollectImageAdapter.setNewData(item.products);
    }

    public interface ShopCartCollectAdapterListener {
        void onCheckShop(int position, boolean isCheck,String saleType);
    }
}
